package com.AAL.DAO;

import java.util.List;

import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IDealerDAO;
import com.AAL.Model.Dealer;
import com.AAL.Model.StatusOfAccount;
import com.AAL.Utils.HibernateUtil;

public class DealerDAO implements IDealerDAO {

	@Override
	public Dealer saveAndUpdateDealer(Dealer dealer) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			/*System.out.println("Ac:"+ dealer.getAccount().getPassword());
			session.saveOrUpdate(dealer.getAccount());
			System.out.println("Information:"+ dealer.getAccount().getAccountInformation());
			session.saveOrUpdate(dealer.getAccount().getAccountInformation());
			*/
			System.out.println("De:"+ dealer);
			session.saveOrUpdate(dealer);
		} catch (Exception e) {
			System.out.println("Loi: "+ e.getMessage());
			return null;
		}
		session.getTransaction().commit();
		return dealer;
	}

	@Override
	public List<Dealer> getAllLstDealer() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Dealer> lstDealer = null;
		try {
			lstDealer = (List<Dealer>) session.createQuery("from Dealer").list();
			System.out.println(lstDealer);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return lstDealer;
	}

	@Override
	public List<Dealer> getLstDealerByStatus(StatusOfAccount statusOfAccount) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Dealer getDealer(int dealerID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Dealer dealer = (Dealer) session.load(Dealer.class, dealerID);
		return dealer;
	}

	@Override
	public Dealer deleteDealer(int dealerID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			Dealer dealer = (Dealer) session.load(Dealer.class, dealerID);
			if (null != dealer) {
				session.delete(dealer);
			}
			session.getTransaction().commit();
			return dealer;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
	
	}
}
